﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Windows.Forms;
using HREDU.Hoang_Tuan.Reports;
using HREDU.Hoang_Tuan.Classes;

using Janus.Windows.GridEX;
using System.Drawing.Drawing2D;


namespace HREDU.Hoang_Tuan
{
    public partial class frmDSNangLuong : Form
    {
        //private ListViewColumnSorter lvwColumnSorter;
        public Form fCallForm;

        //Dau vao ten cac column se xu ly trong treeview va listview
        private string nhomColumnName = CDONVICollection.nhomColumnName;
        private string tenColumnName = CDONVICollection.tenColumnName;
        private string cuoiColumnName = CDONVICollection.cuoiColumnName;

        public DataSet dsLuong;

        #region "New"
        
        public frmDSNangLuong()
        {
            InitializeComponent();
        }

        public frmDSNangLuong(Form frmCall)
        {

            // This call is required by the Windows Form Designer.
            InitializeComponent();

            // Add any initialization after the InitializeComponent() call.
            fCallForm = frmCall;
        }
        #endregion

        private void frmDSNangLuong_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'hREDUDataSet.view_DSNangLuong' table. You can move, or remove it, as needed.
            InitTreeView();
            tvControl.ExpandAll();
            tvControl.SelectedNode = tvControl.Nodes[0];

           
            //Load Chế độ hiển thị
            cmbHienThi.BeginUpdate();
            cmbHienThi.Items.Add("Danh sách nâng lương đợt 1");
            cmbHienThi.Items.Add("Danh sách nâng lương đợt 2");
            cmbHienThi.Items.Add("Danh sách thâm niên vượt khung đợt 1");
            cmbHienThi.Items.Add("Danh sách thâm niên vượt khung đợt 2");
            cmbHienThi.EndUpdate();

            //Add list nam nang luong
            int num = DateTime.Now.Year;//Xet khoang 10 nam nang luong
            cmbNamXepLuong.Items.Add("");
            for (int i = 0; i < 10; i++)
            {
                cmbNamXepLuong.Items.Add(num.ToString());
                num -= 1;
            }
            
            //InitDataGridView();
            InitDataGridJanus();

            //Mac dinh nam xep luong bang nam hien tai
            cmbNamXepLuong.SelectedIndex = 1;

            //Hien thi danh muc don vi
            chkShowDMDonVi.Checked = true;
        }
        
        #region "tvControl"

        #region "tvControl Functions"

        //Add node dau tien cua treeview
        public void FormatNode(ref TreeNode node)
        {
            node.ForeColor = Color.Blue;
            node.ImageIndex = 0;
            node.SelectedImageIndex = 0;
        }

        public void FormatLeafNode(ref TreeNode node)
        {
            node.ForeColor = Color.DarkRed;
            node.ImageIndex = 1;
            node.SelectedImageIndex = 0;
        }

        public void InitTreeView()
        {
            tvControl.ImageList = ImageList1;
            //Lay danh muc tree view dau tien
            string strSQL;

            //Nhom DON VI
            strSQL = "select * from DONVI where left(nhom,3) = '002' and len(nhom) = (select min(len(nhom)) from DONVI)";

            DataSet ds = MITI.BaseDB.Instance.ExecSql_DataSet(strSQL);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                //Add Node in Treeview
                TreeNode node = tvControl.Nodes.Add(dr[nhomColumnName].ToString(), dr[tenColumnName].ToString());

                //Kiem tra node co phai la node dau cua treeview khong
                //Neu bang thi cho mau xanh, nguoc lai mau do
                //Tra ve node dau tien theo format dinh san
                if (((bool)dr[cuoiColumnName] == false))
                    FormatNode(ref node);
                else  //Tra ve node leaf theo format dinh san
                    FormatLeafNode(ref node);
            }
            tvControl.Focus();
            tvControl.SelectedNode = tvControl.Nodes[0];
        }

        public void LoadTreeView(string nhomCha)
        {
            //Tim node co theo ten va tra ve kq
            TreeNode[] nodes = tvControl.Nodes.Find(nhomCha, true);
            TreeNode node;

            //Neu khong co node nao tim thay
            if (nodes.Length == 0)
                return;

            //Lay node dau tien trong cac node tim thay
            node = nodes[0];

            //Neu node chon da co cac node con roi thi thoat
            if (node.Nodes.Count > 0)
                return;

            //Lay cac node con theo node can tim ma khong loc theo thuoc tinh cuoi
            DataTable db = CDONVICollection.LayCoQuanTheoNhomCha_DataTable(nhomCha);
            //Neu co node can tim co cac node con
            if (db.Rows.Count > 0)
            {
                foreach (DataRow dr in db.Rows)
                {
                    //Add Node in Treeview
                    TreeNode childNode = node.Nodes.Add(dr[nhomColumnName].ToString(), dr[tenColumnName].ToString());

                    //Kiem tra node co phai la node dau cua treeview khong
                    //Neu bang thi cho mau xanh, nguoc lai mau do
                    //Tra ve node dau tien theo format dinh san
                    if (((bool)dr[cuoiColumnName] == false))
                        FormatNode(ref childNode);
                    else  //Tra ve node leaf theo format dinh san
                        FormatLeafNode(ref childNode);
                }
            }
        }

        #endregion

        private void tvControl_AfterSelect(object sender, TreeViewEventArgs e)
        {
            LoadTreeView(tvControl.SelectedNode.Name);
            LoadDataGridJanus();
            lblDonVi.Text = tvControl.SelectedNode.Text;

            chkCheckAll.Checked = false;
        }
        #endregion

        #region "DataGrid Janus 2.0"

        public void InitDataGridJanus()
        {
            LoadDataGridJanus();

            //Style GridEX
            this.dtgDSNhanSu.FlatBorderColor = System.Drawing.SystemColors.ControlDark;
            this.dtgDSNhanSu.GridLineColor = System.Drawing.SystemColors.ScrollBar;
            this.dtgDSNhanSu.GroupRowVisualStyle = Janus.Windows.GridEX.GroupRowVisualStyle.Outlook2003;
            this.dtgDSNhanSu.SelectedFormatStyle.Blend = 0.5F;
            this.dtgDSNhanSu.SelectedFormatStyle.ForeColor = System.Drawing.SystemColors.WindowText;
            this.dtgDSNhanSu.VisualStyleAreas.CheckBoxStyle = Janus.Windows.GridEX.VisualStyle.Standard;
            this.dtgDSNhanSu.VisualStyleAreas.ControlBorderStyle = Janus.Windows.GridEX.VisualStyle.Standard;
            this.dtgDSNhanSu.VisualStyleAreas.EditControlsStyle = Janus.Windows.GridEX.VisualStyle.Standard;
            this.dtgDSNhanSu.VisualStyleAreas.GroupByBoxStyle = Janus.Windows.GridEX.VisualStyle.Standard;
            this.dtgDSNhanSu.VisualStyleAreas.GroupRowsStyle = Janus.Windows.GridEX.VisualStyle.Standard;
            this.dtgDSNhanSu.VisualStyleAreas.HeadersStyle = Janus.Windows.GridEX.VisualStyle.Office2003;
            this.dtgDSNhanSu.VisualStyleAreas.ScrollBarsStyle = Janus.Windows.GridEX.VisualStyle.Standard;
            this.dtgDSNhanSu.VisualStyleAreas.TreeGliphsStyle = Janus.Windows.GridEX.VisualStyle.Standard;

            //Group
            dtgDSNhanSu.RetrieveStructure();
            dtgDSNhanSu.RootTable.Groups.Add(new Janus.Windows.GridEX.GridEXGroup(dtgDSNhanSu.RootTable.Columns["TEN_DONVI"]));
            //dtgDSNhanSu.RootTable.Groups.Add(new Janus.Windows.GridEX.GridEXGroup(dtgDSNhanSu.RootTable.Columns["NAMLUONG"]));
            dtgDSNhanSu.RootTable.GroupRowFormatStyle.ForeColor = Color.DarkBlue;
            dtgDSNhanSu.RootTable.GroupRowFormatStyle.FontBold = TriState.True;
            
            dtgDSNhanSu.GroupByBoxVisible = false;
            dtgDSNhanSu.ThemedAreas = ThemedArea.All;

            FormatDataGridJanus();
        }

        public void EnablesColumnDataGridJanus(bool isNangLuong)
        {
            if (dtgDSNhanSu.RootTable != null)
            {
                //NANG LUONG
                //dtgDSNhanSu.RootTable.Columns["ten_ngach2"].Visible = isNangLuong;
                dtgDSNhanSu.RootTable.Columns["ten_bac2"].Visible = isNangLuong;
                dtgDSNhanSu.RootTable.Columns["heso2"].Visible = isNangLuong;

                //THAM NIEN VUOT KHUNG
                dtgDSNhanSu.RootTable.Columns["PHUCAPTNVK"].Visible = !isNangLuong;
            }

        }

        /// <summary>
        /// Load tat ca danh sach nhan vien co NamNangLuong <= NamXetLuong - SoNamGiuBac
        /// </summary>
        public void LoadDataGridJanus()
        {
            if (cmbHienThi.Items.Count == 0)
                return;

            string nhom_donvi = tvControl.SelectedNode.Name;
            
            int namxepluong = -1;
            if (cmbNamXepLuong.Text != "")
                namxepluong = int.Parse(cmbNamXepLuong.Text);

            dsLuong = CNHANVIENCollection.LayDSNangLuongTheo_NhomDONVI(nhom_donvi, 1, namxepluong);
            DataView dv = dsLuong.Tables[0].DefaultView;
            
            switch (cmbHienThi.SelectedIndex)
            {
                case 0:
                    dsLuong = CNHANVIENCollection.LayDSNangLuongTheo_NhomDONVI(nhom_donvi, 1, namxepluong);
                    dsLuong.Tables[0].TableName = "view_DSNangLuong";
                    dv = dsLuong.Tables[0].DefaultView;
                    dtgDSNhanSu.DataSource = dv;
                    EnablesColumnDataGridJanus(true);
                    break;
                case 1:
                    dsLuong = CNHANVIENCollection.LayDSNangLuongTheo_NhomDONVI(nhom_donvi, 2, namxepluong);
                    dsLuong.Tables[0].TableName = "view_DSNangLuong";
                    dv = dsLuong.Tables[0].DefaultView;
                    dtgDSNhanSu.DataSource = dv;
                    EnablesColumnDataGridJanus(true);
                    break;
                case 2:
                    dsLuong = CNHANVIENCollection.LayDSPCThamNienVuotKhungTheo_NhomDONVI(nhom_donvi, 1, namxepluong);
                    dsLuong.Tables[0].TableName = "view_DSPCThamNienVuotKhung";
                    dv = dsLuong.Tables[0].DefaultView;
                    dtgDSNhanSu.DataSource = dv;
                    EnablesColumnDataGridJanus(false);
                    break;
                case 3:
                    dsLuong = CNHANVIENCollection.LayDSPCThamNienVuotKhungTheo_NhomDONVI(nhom_donvi, 2, namxepluong);
                    dsLuong.Tables[0].TableName = "view_DSPCThamNienVuotKhung";
                    dv = dsLuong.Tables[0].DefaultView;
                    dtgDSNhanSu.DataSource = dv;
                    EnablesColumnDataGridJanus(false);
                    break;
                default:
                    cmbHienThi.SelectedIndex = 0;
                    break;
            }
            TongSoToolStripStatusLabel.Text = "Có tất cả " + dv.Count.ToString() + " người";
        }

        public void FormatDataGridJanus()
        {
            CultureInfo culture = new CultureInfo("fr-FR", true);

            //Set visible = false cho tat ca cac thuoc
            for (int i = 0; i < dtgDSNhanSu.RootTable.Columns.Count; i++)
            {
                dtgDSNhanSu.RootTable.Columns[i].Visible = false;
                dtgDSNhanSu.RootTable.Columns[i].EditType = Janus.Windows.GridEX.EditType.NoEdit;
            }

            //Add a checkbox column
            //AddCheckBoxColumnJanus_ChonIn();
           
            //Cac thuoc tinh chung
            dtgDSNhanSu.RootTable.Columns["MACBCC"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["MACBCC"].Caption = "Mã";
            dtgDSNhanSu.RootTable.Columns["MACBCC"].Width = 60;

            dtgDSNhanSu.RootTable.Columns["TEN_KHAISINH"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["TEN_KHAISINH"].Caption = "Họ và tên khai sinh";
            dtgDSNhanSu.RootTable.Columns["TEN_KHAISINH"].Width = 200;
            

            dtgDSNhanSu.RootTable.Columns["NGAYSINH"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["NGAYSINH"].Caption = "Ngày sinh";
            dtgDSNhanSu.RootTable.Columns["NGAYSINH"].FormatString = culture.DateTimeFormat.ShortDatePattern;
            dtgDSNhanSu.RootTable.Columns["NGAYSINH"].Width = 73;

            dtgDSNhanSu.RootTable.Columns["GIOITINH"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["GIOITINH"].Caption = "Giới tính";
            dtgDSNhanSu.RootTable.Columns["GIOITINH"].Width = 55;

            dtgDSNhanSu.RootTable.Columns["ChucVuChinhQuyen"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["ChucVuChinhQuyen"].Caption = "Chức vụ";
            dtgDSNhanSu.RootTable.Columns["ChucVuChinhQuyen"].Width = 200;

            dtgDSNhanSu.RootTable.Columns["trinhdochuyenmon"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["trinhdochuyenmon"].Caption = "Trình độ CMNV";
            dtgDSNhanSu.RootTable.Columns["trinhdochuyenmon"].Width = 120;

            dtgDSNhanSu.RootTable.Columns["NAMXEPLUONG"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["NAMXEPLUONG"].Caption = "Năm xếp lương";
            dtgDSNhanSu.RootTable.Columns["NAMXEPLUONG"].FormatString = "MM/yyyy";
            dtgDSNhanSu.RootTable.Columns["NAMXEPLUONG"].TextAlignment = TextAlignment.Center;

            dtgDSNhanSu.RootTable.Columns["ten_ngach"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["ten_ngach"].Caption = "Ngạch lương";
            dtgDSNhanSu.RootTable.Columns["ten_ngach"].Width = 100;

            dtgDSNhanSu.RootTable.Columns["ten_bac"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["ten_bac"].Caption = "Bậc";
            dtgDSNhanSu.RootTable.Columns["ten_bac"].TextAlignment = TextAlignment.Center;
            dtgDSNhanSu.RootTable.Columns["ten_bac"].Width = 40;

            dtgDSNhanSu.RootTable.Columns["heso"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["heso"].Caption = "Hệ số";
            dtgDSNhanSu.RootTable.Columns["heso"].FormatString = GeneralCode.FormatQuantityStyle();
            dtgDSNhanSu.RootTable.Columns["heso"].TextAlignment = TextAlignment.Far;
            dtgDSNhanSu.RootTable.Columns["heso"].Width = 45;

            dtgDSNhanSu.RootTable.Columns["PHUCAPTNVK"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["PHUCAPTNVK"].Caption = "PC TNVK(%)";
            dtgDSNhanSu.RootTable.Columns["PHUCAPTNVK"].TextAlignment = TextAlignment.Far;
            dtgDSNhanSu.RootTable.Columns["PHUCAPTNVK"].Width = 80;

            dtgDSNhanSu.RootTable.Columns["ten_bac2"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["ten_bac2"].Caption = "Bậc kế";
            dtgDSNhanSu.RootTable.Columns["ten_bac2"].TextAlignment = TextAlignment.Center;
            dtgDSNhanSu.RootTable.Columns["ten_bac2"].Width = 50;

            dtgDSNhanSu.RootTable.Columns["heso2"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["heso2"].Caption = "Hệ số kế";
            dtgDSNhanSu.RootTable.Columns["heso2"].FormatString = GeneralCode.FormatQuantityStyle();
            dtgDSNhanSu.RootTable.Columns["heso2"].TextAlignment = TextAlignment.Far;
            dtgDSNhanSu.RootTable.Columns["heso2"].Width = 55;

            dtgDSNhanSu.RootTable.Columns["TEN_LYDONL"].Visible = true;
            dtgDSNhanSu.RootTable.Columns["TEN_LYDONL"].Caption = "Lý do nâng lương";
            dtgDSNhanSu.RootTable.Columns["TEN_LYDONL"].Width = 150;

            //group
            dtgDSNhanSu.RootTable.Columns["TEN_DONVI"].Caption = "Đơn vị:";
            dtgDSNhanSu.RootTable.Columns["TEN_DONVI"].CharacterCasing = CharacterCasing.Upper;
            dtgDSNhanSu.RootTable.Columns["TEN_DONVI"].HeaderStyle.ForeColor = Color.Red;
            //dtgDSNhanSu.RootTable.Columns["NAMLUONG"].Caption = "Năm xếp lương";
        }

        //Ham them column check box vao DataGridView
        private void AddCheckBoxColumnJanus_ChonIn(){
            GridEXColumn column = new GridEXColumn();
            column.ColumnType = Janus.Windows.GridEX.ColumnType.CheckBox;
            column.EditType = Janus.Windows.GridEX.EditType.CheckBox;

            //column.EditTarget = Janus.Windows.GridEX.EditTarget.Value;
            //column.EditButtonDisplayMode = CellButtonDisplayMode.EditingCell;

            //checkbox setting
            column.CheckBoxTrueValue = true;
            column.CheckBoxFalseValue = false;
            column.CheckBoxTriState = false;
            column.CheckBoxNullValue = null;

			column.Key = "isCheck";
            column.DataMember = "isCheck";
			column.Caption = "Chọn In";
			column.Width = 50;
            column.Visible = true;
            try
            {
                dtgDSNhanSu.RootTable.Columns.Insert(0, column);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void dtgDSNhanSu_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            btnChon_Click(null,null);
            btnRefresh_Click(null, null);
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChon_Click(object sender, EventArgs e)
        {
            //Neu FORM duoc goi tu frmNHANSU cho viec copy thong tin cua mot ai do de them nhan su moi
            //Nguoc lai la goi frmNHANSU tu form frmDSNHANSU de cap nhat thong tin
            Janus.Windows.GridEX.GridEXRow row = dtgDSNhanSu.GetRow();

            //Neu RowType la record thi moi tiep tuc
            if (row.RowType == Janus.Windows.GridEX.RowType.Record)
            {
                //fCallForm != null cho biết là tạo mới NHANVIEN bằng thông tin của NHANVIEN khác
                if (fCallForm != null)
                {
                    frmNHANSU frm = (frmNHANSU)fCallForm;
                    frm.objNHANVIEN = CNHANVIENCollection.LayNhanVienTheoID((int)row.Cells["id_nhanvien"].Value);
                    frm.FillFormNHANSU();
                    this.Close();
                }
                else
                {
                    frmNHANSU frm = new frmNHANSU(this, (int)row.Cells["id_nhanvien"].Value, false);
                    frm.ShowDialog();
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbNamXepLuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataGridJanus();
        }

        private void cmbHienThi_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataGridJanus();
        }

        //private void cmbHienThi_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    ChonCheDoHienThiDataGridJanus();
        //}
        /// <summary>
        /// Lay chuoi SQL dieu kien loc danh sach
        /// </summary>
        /// <returns></returns>
        //private string LayDieuKienLoc()
        //{
        //    string mySqlDK = "";
        //    if (tvControl.SelectedNode.Name != "")
        //    {
        //        string nhom = tvControl.SelectedNode.Name;
        //        mySqlDK = "where left(nhom, " + nhom.Length.ToString() + ") = '" + nhom + "'";
        //    }

        //    if (cmbNamXepLuong.Text != "")
        //    {
        //        int iNamNhanLuong = int.Parse(cmbNamXepLuong.Text);
        //        if (mySqlDK == "")
        //            mySqlDK = " where namluong2 <= " + iNamNhanLuong.ToString();
        //        else
        //            mySqlDK += " and namluong2 <= " + iNamNhanLuong.ToString();
        //    }

        //    return mySqlDK;
        //}

        /// <summary>
        /// Thuc hien in danh sach dang chon tren form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnIn_Click(object sender, EventArgs e)
        {
            //GeneralCode.OutputText(dtgDSNhanSu);
            //string strName = "";
            //switch (cmbHienThi.SelectedIndex)
            //{
            //    case 0:
            //        strName = "view_DSNangLuong";
            //        break;
            //    case 1:
            //        strName = "view_DSNangLuong";
            //        break;
            //    case 2:
            //        strName = "view_DSPCThamNienVuotKhung";
            //        break;
            //    case 3:
            //        strName = "view_DSPCThamNienVuotKhung";
            //        break;
            //}

            ////string strSQL = "select * from " + strName + " " + LayDieuKienLoc();

            ////DataSet ds = MITI.BaseDB.Instance.ExecSql_DataSet(strSQL);

            ////ds.Tables[0].TableName = strName;

            //dsLuong.Tables[0].TableName = strName;

            frmINDSNANGLUONG frm = new frmINDSNANGLUONG(this, dsLuong, cmbHienThi.SelectedIndex);
            frm.ShowDialog();
        }

        /// <summary>
        /// Chon check tat ca hay uncheck tat ca
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (Janus.Windows.GridEX.GridEXRow row1 in dtgDSNhanSu.GetRows())
                foreach (Janus.Windows.GridEX.GridEXRow row2 in row1.GetChildRows())
                    foreach (Janus.Windows.GridEX.GridEXRow r in row2.GetChildRows())
                {
                    //Neu RowType la record thi moi tiep tuc
                    if (r.RowType == Janus.Windows.GridEX.RowType.Record)
                    {
                        r.BeginEdit();
                        r.Cells["isCheck"].Value = true;
                        r.EndEdit();
                    }
                }
        }

        private void chkShowDMDonVi_CheckedChanged(object sender, EventArgs e)
        {
            if (chkShowDMDonVi.Checked)
            {
                panel2.Visible = true;
            }
            else
                panel2.Visible = false;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadDataGridJanus();
        }
    }
}